
// 1. 基本功能、异步调用
// 2. 多次调用then
// 3. then的链式调用
// 4. 异步链式调用

// 5. 自调用报错提示
// 6. 内部健壮性测试，报错抛出

// 7. 原型 all方法
// 8. 本身resolve方法、reject
// 9. 原型 finally方法
// 10. catch

const FULFILLED = 'fulfilled'
const REJECTED = 'rejected'
const PENDING = 'pending'

class MyPromise {
    constructor (executor) {
        try {
            executor(this.resolve, this.reject)
        }
        catch (err) {
            this.reject(err)
        }
    }

    status = PENDING
    successCallback = []
    failCallback = []
    value = undefined
    reason = undefined

    resolve = value => {
        if (this.status !== 'pending') return 
        this.status = FULFILLED
        this.value = value
        while (this.successCallback.length) {
            this.successCallback.shift()()
        }
    }

    reject = reason => {
        if (this.status !== 'pending') return 
        this.status = REJECTED
        this.reason = reason
        while (this.failCallback.length) {
            this.failCallback.shift()()
        } 
    }

    then (successCallback, failCallback) {
        let promise2 = new MyPromise((resolve, reject) => {
            if (this.status === FULFILLED) {
                setTimeout(() => {
                    try {
                        let x = successCallback(this.value)
                        resolvePromise(promise2, x, resolve, reject)
                    } 
                    catch (err) {
                        reject(err)
                    }
                }, 0);
            } else if (this.status === REJECTED) {
                setTimeout(() => {
                    try {
                        let x = failCallback(this.reason)
                        resolvePromise (promise2, x, resolve, reject)
                    }
                    catch (err) {
                        reject(err)
                    }
                }, 0);
            } else {
                this.successCallback.push(() => {
                    setTimeout(() => {
                        try {
                            let x = successCallback(this.value)
                            resolvePromise (promise2, x, resolve, reject)
                        } catch (err) {
                            reject(err)
                        }
                    }, 0);
                })
                this.failCallback.push(() => {
                    setTimeout(() => {
                        try {
                            let x = failCallback(this.reason)
                            resolvePromise (promise2, x, resolve, reject)
                        } catch (err) {
                            reject(err)
                        }
                    }, 0);
                })
            }
        })
        return promise2
    }

    catch (reject) {
        return this.then(null, reject)
    }

    finally (callback) {
        return this.then((resolve, reject) => {
            let x = callback()      
            if (x instanceof MyPromise) {
                x.then(resolve, reject)
            } else {
                resolve(x)
            }
        })
    }

    static all (array) {
        let result = []
        let num = 0;
        return new MyPromise((resolve, reject) => {
            function addData (key, value) {
                result[key] = value;
                num++
                if (num === array.length) {
                    resolve(result)
                }
            }
            for (let i = 0; i < array.length; i++) {
                if (array[i] instanceof MyPromise) {
                    array[i].then((value) => {
                        addData(i, value);
                    }, reason => {
                        reject(reason)
                    })
                } else {
                    addData(i, array[i]);
                }
            }
        })
    }

    static race (array) {
        return new MyPromise((resolve, reject) => {
            for (let i = 0; i < array.length; i++) {
                if (array[i] instanceof MyPromise) {
                    array[i].then((value) => {
                        resolve(value)
                    }, reason => {
                        reject(reason)
                    })
                } else {
                    resolve(array[i])
                }
            }
        })
    }

    static resolve (value) {
        return new MyPromise((resolve, reject) => {
            resolve(value)
        })
    }

    static reject (reason) {
        return new MyPromise((resolve, reject) => {
            reject(reason)
        })
    }
}

function resolvePromise (promise2, x, resolve, reject) {
    if (promise2 === x) {
        return reject(new TypeError('Chaining cycle detected for promise'))
    }
    if (x instanceof MyPromise) {
        x.then(resolve, reject)
    } else {
        resolve(x)
    }
}


module.exports = MyPromise